<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-08-20
 * Time: 14:06
 */
//用户登录登出操作
class b2c_ctl_wap_personal extends b2c_ctl_wap_common{
    function __construct(&$app){
        parent::__construct($app);
        $shopname = app::get('wap')->getConf('wap.name');
        $this->shopname =$shopname;
        if(isset($shopname)){
            $this->title = app::get('b2c')->_('樱淘社').$shopname;
            $this->keywords = app::get('b2c')->_('樱淘社').$shopname;
            $this->description = app::get('b2c')->_('樱淘社').$shopname;
        }
        $this->action = $this->_request->get_act_name();
        if(!$this->action) $this->action = 'index';
        $this->pagedata['res_url'] = $this->app->res_url;
        $this->userObject = kernel::single('b2c_user_object');
        /** end **/
    }
//用户中心首页加载
    function index (){
        if($this->userObject->is_login() ){
            $this->logged();
        }else{
            $this->pagedata['logo'] = '个人中心';
            $this->page('wap/personal/index.html');
        }
    }
//设置页加载
    function set (){
        $this->pagedata['logo'] = '设置';
        $this->pagedata['seturl'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'logout'));
        $this->pagedata['indexurl'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'index'));
        $this->page('wap/personal/set.html');
    }
//登陆页加载
    function loginpage (){
        $this->pagedata['logo'] = '登录';
        $this->pagedata['url'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'login'));
        $this->pagedata['urls'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'logged'));
        $this->pagedata['rurl'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'register'));
        $this->page('wap/personal/login.html');
    }
//处理登陆请求
    function login (){
        $input['method'] = array("file"=>"mobileapi_rpc_passport","action"=>"post_login");
        $input['data'] = array("uname"=>$_POST['uname'],'password'=>$_POST['password']);
        $res = $this->getApi($input);
        echo json_encode($res);
    }
//    用户登出
    function logout() {
        session_start();
        $input['method'] = array("file"=>"mobileapi_rpc_passport","action"=>"logout");
        $res = $this->getApi($input);
//        kernel::single('b2c_ctl_wap_passport')->unset_member();
        header('location:'.kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'index')));
    }
//注册页加载
    function register (){
        $this->pagedata['logo'] = '注册';
        $this->pagedata['url'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'getmsg'));
        $this->pagedata['urls'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'toregister'));
        $this->pagedata['rurl'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'logged'));
        $this->page('wap/personal/register.html');
    }
//注册页获取短信
    function getmsg (){
        $input['method'] = array("file"=>"mobileapi_rpc_passport","action"=>"send_vcode_sms");
        if(empty($_POST['type'])){
            $input['data'] = array("uname"=>$_POST['uname']);
        }else{
            $input['data'] = array("uname"=>$_POST['uname'],'type'=>$_POST['type']);
        }
        $res = $this->getApi($input);
    }
//注册页处理注册请求
    function toregister() {
        $input['method'] = array("file"=>"mobileapi_rpc_passport","action"=>"member_create");
        $input['data'] = array("uname"=>$_POST['uname'],"password"=>$_POST['password'],"vcode"=>$_POST['vcode']);
        $res = $this->getApi($input);
        echo json_encode($res);
    }
//已登录页加载
    function logged (){
        /*if(empty($_GET['mid'])){
            header('location:'.kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'index')));
        }*/
        $mid = $this->get_current_member();
        $input['method'] = array("file"=>"mobileapi_rpc_goods","action"=>"get_member_info");
        $input['data'] = array("member_id"=>$mid['member_id']);
        $res = $this->getApi($input);
        $this->pagedata['member_id'] = $_GET['mid'];//修改
        $this->pagedata['avatar'] = $res['data']['avatar'];//头像
        $this->pagedata['name'] = $res['data']['name'];//名字
        $this->pagedata['sex'] = $res['data']['sex'];//性别0女1男2其他
        $this->pagedata['member_lv_id'] = $res['data']['member_lv_id'];//等级
        $this->pagedata['desc'] = $res['data']['desc'];//签名
        $this->pagedata['point'] = $res['data']['point'];//樱磅
        $this->pagedata['messagecount'] = $res['data']['messagecount'];//未读消息

        $this->page('wap/personal/logged.html');
    }
//加载忘记密码页
    function resetpage(){
        $this->pagedata['url'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'getmsg'));
        $this->pagedata['curl'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'resetcode'));
        $this->pagedata['indexurl'] = kernel::base_url(1).$this->gen_url(array('app'=>'b2c','ctl'=>'wap_personal','act'=>'index'));
        $this->page('wap/personal/resetpage.html');
    }

//    忘记密码
    function resetcode(){
        $input['method'] = array("file"=>"mobileapi_rpc_passport","action"=>"resetpassword");
        $input['data'] = array("account"=>$_POST['phone'],"key"=>$_POST['key'],"login_password"=>$_POST['password'],"psw_confirm"=>$_POST['password']);
        $res = $this->getApi($input);
    }

//    第三方登陆
    function thirdlogin(){
        $input['method'] = array("file"=>"mobileapi_rpc_passport","action"=>"trust_login");
        $input['data'] = array("provider_code"=>$_POST['phone'],"openid"=>$_POST['key'],"login_password"=>$_POST['password'],"psw_confirm"=>$_POST['password']);
        $res = $this->getApi($input);
    }
}